#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/6/9 15:52
# @Author : linjinting
# @Project : custom_protocol
# @File : mysql_handle.py
# @Software: PyCharm

from mysql_model.mysql_connection import MysqlConnection


class HandleBase(object):
    def __init__(self, config_=None, **kwargs):
        if config_ is None:
            return
        self.m_config = config_
        self.m_connection = MysqlConnection(self.m_config, **kwargs)
        self.m_connection.connect()
        self.m_tableName = None

    def connect(self):
        self.m_connection.connect()

    def isConnected(self):
        return self.m_connection.isConnected()

    def set_connection(self, conn):
        self.m_connection = conn

    def get_connection(self):
        return self.m_connection

    def get_data(self, tableName, fields_):
        if not self.m_connection.isConnected():
            self.m_connection.connect()
        _sql, _values = MysqlConnection.genSelectSQL(tableName, fields_)
        result = self.m_connection.select(_sql, _values)
        return result


class Station(HandleBase):
    def __init__(self, config_=None):
        super().__init__(config_)
        # self.m_stationList = list()

    def get_station(self):
        tableName = "t_station"
        fields_ = ['ID', 'number', 'NAME']
        return self.get_data(tableName, fields_)
        # filters_ = [['ID', '1833', '=']]

        # if result:
        #     # logger.debug(result)
        #     for res in result:
        #         self.m_stationList.append(res['ID'])


class Device(HandleBase):
    def __init__(self, config_=None):
        super().__init__(config_)

    def get_device(self):
        tableName = "t_device"
        fields_ = ['ID', 'stationID', 'number', 'deviceTypeID']
        return self.get_data(tableName, fields_)


class DeviceType(HandleBase):
    def __init__(self, config_=None):
        super().__init__(config_)

    def get_deviceType(self):
        tableName = "t_devicetype"
        fields_ = ['ID', 'name']
        return self.get_data(tableName, fields_)
